package com.pkx.cloud.fse7.dms.openrecord.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pkx.cloud.fse7.dms.openrecord.entity.OpenRecord;
import com.pkx.cloud.fse7.dms.openrecord.service.OpenRecordService;
import com.pkx.cloud.whzx.comn.ORpose;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author chenwg
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/openrecord/open-record")
@Api(value = "开门记录", tags = "开门记录")
@Slf4j
@ORpose
public class OpenRecordController {

    @Autowired
    private OpenRecordService openRecordService;

    @ApiOperation("分页列表")
    @GetMapping("/page")
    public Page<OpenRecord> page(Page page, OpenRecord openRecord) throws ParseException {
        return openRecordService.pageList(page, openRecord);
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    public List<OpenRecord> list(OpenRecord openRecord) {
        return openRecordService.listByEntity(openRecord);
    }

    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public OpenRecord selectById(@PathVariable("id") Long id) {
        return openRecordService.selectById(id);
    }

    @ApiOperation("分页列表")
    @GetMapping("/pageList")
    public Page<OpenRecord> pageList(Page page, OpenRecord openRecord) throws ParseException {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = null;
        LocalDateTime endTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0);
        Integer index = openRecord.getIndex();
        switch (openRecord.getType()) {
            // 当日
            case 0:
                startTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), index, 0, 0);
                endTime = startTime.plusHours(1);
                break;
            //当月
            case 1:
                startTime = LocalDateTime.of(now.getYear(), now.getMonth(), index + 1, 0, 0, 0);
                endTime = startTime.plusDays(1).minusSeconds(1);
                break;
            // 当年
            case 2:
                startTime = LocalDateTime.of(now.getYear(), index + 1, 1, 0, 0, 0);
                endTime = startTime.plusMonths(1).minusSeconds(1);
                break;
            default:
                new RuntimeException("参数错误");
        }
        openRecord.setStartTime(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        openRecord.setEndTime(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return openRecordService.pageList(page, openRecord);
    }


    @GetMapping("/byAId/{id}")
    public Map getByRecordAId(@PathVariable(value = "id") Long id) {
       return  openRecordService.getByRecordAId(id);
    }


    @GetMapping("/byId/{id}")
    @ApiOperation("查询开门卡类型")
    public String getByRecordId(@PathVariable(value = "id") Long id) {
        String byRecordId  = openRecordService.getByRecordId(id);
        return byRecordId;
    }
}
